/*
#
#  Copyright 2011 Matthew Jannace
#
#  This file is part of RoboCode BlackHoleAgent
#
#  RoboCode BlackHoleAgent is free software: you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  RoboCode BlackHoleAgent is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with RoboCode BlackHoleAgent. If not, see
#  <http://www.gnu.org/licenses/>.
#
 */
package com.jannaceltd.TeamBlackHoleBeer.Modules.CustomEvents;

import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Abstract;
import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Interface;
import com.jannaceltd.TeamBlackHoleBeer.Modules.HelperClasses.Enemy;
import com.jannaceltd.TeamBlackHoleBeer.Modules.HelperClasses.TeamMate;
import java.awt.geom.Point2D;
import robocode.Condition;

/**
 * Custom Event Condition to Test if a Robot Distance Buffer Condition is Respected
 * @author Matthew Jannace <mjannace@gmail.com>
 * @version 1.0
 */
public class Robot_DistanceBufferCondition_Respected extends Condition implements BlackHole_Interface {

    private BlackHole_Abstract Agent;
    private Point2D.Double myPos;

    /**
     * Creates A Custom Event Condition to Test if a Robot Distance Buffer Condition is Respected
     * @param Agent
     * @see CustomEventProcessor
     */
    public Robot_DistanceBufferCondition_Respected(BlackHole_Abstract Agent) {
        this.Agent = Agent;
    }

    /**
     * Test if the Robot Distance Buffer Condition is Respected
     * @return True if Robot Distance Buffer Condition IS Respected else False
     * @see CustomEventProcessor
     */
    @Override
    public boolean test() {
        myPos = Agent.getPos();

        if (TeamMateDistanceRespected() && EnemyDistanceRespected()) {
            return true;
        }
        return false;
    }

    /**
     * Checks if any Robot TeamMate are less than the
     * DISTANCE_BUFFER_RESPECTED range
     * @return True if Bullet Distance Buffer Condition Is Respected else False
     * @see #test()
     */
    public boolean TeamMateDistanceRespected() {
        for (TeamMate tm : Agent.teamates.values()) {
            if (tm.getAlive() == true) {
                if ((myPos.distance(tm.getPos())) <= DISTANCE_BUFFER_RESPECTED) {
                    return false;
                } else if (Agent.EventProcessor.Violator.containsKey(tm.getName())) {
                    Agent.EventProcessor.Violator.remove(tm.getName());
                }
            }
        }
        return true;
    }

    /**
     * Checks if any Robot Enemies are less than the
     * DISTANCE_BUFFER_RESPECTED range
     * @return True if Bullet Distance Buffer Condition Is Respected else False
     * @see #test()
     */
    public boolean EnemyDistanceRespected() {
        for (Enemy en : Agent.enemies.values()) {
            if (en.getAlive() == true) {
                if ((myPos.distance(en.getPos())) <= DISTANCE_BUFFER_RESPECTED) {
                    return false;
                } else if (Agent.EventProcessor.Violator.containsKey(en.getName())) {
                    Agent.EventProcessor.Violator.remove(en.getName());
                }
            }
        }
        return true;
    }
}
